using System;
using System.IO.Ports;

namespace armcontrolgui
{
	public partial class PortSetting : Gtk.Window
	{
		SerialPort sepo_Com;
		public PortSetting (SerialPort MyPort) : base(Gtk.WindowType.Toplevel)
		{
			if(MyPort == null)
				throw new ArgumentNullException("MyPort");
			
			this.sepo_Com = MyPort;
			
			//Build window
			this.Build ();
			
			//Add all valid port name
			foreach(string StrItem in System.IO.Ports.SerialPort.GetPortNames()){
				if(StrItem == null || StrItem == string.Empty)
					continue;
				
				this.PortSelectBoxEntry.AppendText(StrItem);
			}
			this.PortSelectBoxEntry.Active = 0;
			
			//Add all valid baudrate.
			this.PortSpeedBoxEntry.AppendText("300");
			this.PortSpeedBoxEntry.AppendText("1200");
			this.PortSpeedBoxEntry.AppendText("2400");
			this.PortSpeedBoxEntry.AppendText("4800");
			this.PortSpeedBoxEntry.AppendText("9600");
			this.PortSpeedBoxEntry.AppendText("14400");
			this.PortSpeedBoxEntry.AppendText("19200");
			this.PortSpeedBoxEntry.AppendText("28800");
			this.PortSpeedBoxEntry.AppendText("38400");
			this.PortSpeedBoxEntry.AppendText("57600");
			this.PortSpeedBoxEntry.AppendText("115200");
			//Set Text to current baud.
			this.PortSpeedBoxEntry.Entry.Text = this.sepo_Com.BaudRate.ToString();
			
			//Set Open/Close Button
			this.OpenPortButton.Active = this.sepo_Com.IsOpen;
			this.ClosePortButton.Active = !this.sepo_Com.IsOpen;
			
			//this.sepo_Com.DataBits = 8;
			//this.sepo_Com.StopBits = System.IO.Ports.StopBits.One;
			//this.sepo_Com.Handshake = System.IO.Ports.Handshake.None;
			//this.sepo_Com.Parity = System.IO.Ports.Parity.None;
		}
		
		protected virtual void HideOnDeleteEvent (object o, Gtk.DeleteEventArgs args)
		{
			args.RetVal = this.HideOnDelete();
		}
		
		protected virtual void PortNameChangedEvent (object sender, System.EventArgs e)
		{
			if(this.PortSelectBoxEntry.ActiveText == null || this.PortSelectBoxEntry.ActiveText == string.Empty)
				return;
			this.sepo_Com.PortName = this.PortSelectBoxEntry.ActiveText;
		}
		
		protected virtual void PortSpeedChangedEvent (object sender, System.EventArgs e)
		{
			int SpeedVal = 0;
			if(int.TryParse(this.PortSpeedBoxEntry.ActiveText, out SpeedVal)) {
				this.sepo_Com.BaudRate = SpeedVal;
			} else {
				//TODO log Warning.
			}
		}
		
		protected virtual void PortOpenToggledEvent (object sender, System.EventArgs e)
		{
			if(this.OpenPortButton.Active) {
				this.PortSelectBoxEntry.Sensitive = false;
				this.PortSpeedBoxEntry.Sensitive = false;
				
				try {
					this.sepo_Com.Open();
				} catch (Exception ex) {
					this.ClosePortButton.Active = true;
					this.OpenPortButton.Active = false;
					this.CheckPortSetting();
					
					Console.WriteLine(ex);
					//TODO Log Error.
				}
			}
			
			//Console.WriteLine("Port IsOpen: {0}", this.sepo_Com.IsOpen);
		}
		
		protected virtual void PortCloseToggledEvent (object sender, System.EventArgs e)
		{
			if(this.ClosePortButton.Active) {
				this.sepo_Com.Close();
				
				this.PortSelectBoxEntry.Sensitive = true;
				this.PortSpeedBoxEntry.Sensitive = true;
			}
		}
		
		private void CheckPortSetting()
		{
			string PortName = this.PortSelectBoxEntry.ActiveText;
			int SpeedVal = 0;
			
			if(PortName == null || PortName == string.Empty) {
				this.PortSelectBoxEntry.GrabFocus();
				this.PortSelectBoxEntry.Popup();
			} else if(!int.TryParse(this.PortSpeedBoxEntry.ActiveText, out SpeedVal)) {
				this.PortSpeedBoxEntry.GrabFocus();
				this.PortSpeedBoxEntry.Popup();
			}
		}
		
	}
}
